<?php

class ControleEndereco {
		
	public function cadastrar(Endereco $endereco) {
		
		include_once("../conexao/config.php");
		$conexao = new Config();
		
		$query = "insert into endereco values(NULL, ?, ?, ?, ?, ?)";
		
		$stmt = $conexao->conectar()->prepare($query);
		
		$stmt->bindValue(1, $endereco->getLogradouro());
		$stmt->bindValue(2, $endereco->getBairro());
		$stmt->bindValue(3, $endereco->getCidade());
		$stmt->bindValue(4, $endereco->getEstado());
		$stmt->bindValue(5, $endereco->getCep());
		
		$stmt->execute();
	}
	
	public function alterar(Endereco $endereco) {
		
		include_once("../conexao/config.php");
		$conexao = new Config();
		
		$query = "update endereco set logradouro=?, bairro=?, cidade=?, estado=?, cep=? where id_endereco=?";
		
		$stmt = $conexao->conectar()->prepare($query);
		
		$stmt->bindValue(1, $endereco->getLogradouro());
		$stmt->bindValue(2, $endereco->getBairro());
		$stmt->bindValue(3, $endereco->getCidade());
		$stmt->bindValue(4, $endereco->getEstado());
		$stmt->bindValue(5, $endereco->getCep());
		$stmt->bindValue(6, $endereco->getId());
		
		$stmt->execute();
	}
	
	public function pesquisar($id) {
		
		include_once("../conexao/config.php");
		$conexao = new Config();
		
		$query = "select * from endereco where id_endereco=?";
		
		$stmt = $conexao->conectar()->prepare($query);
		$stmt->bindValue(1, $id);
		
		$stmt->execute();
		
		if($row = $stmt->fetch()) {
			$logradouro = $row['logradouro'];
			$bairro = $row['bairro'];
			$cidade = $row['cidade'];
			$estado = $row['estado'];
			$cep = $row['cep'];	
			
			include_once('endereco.php');
			
			$endereco = new Endereco($logradouro, $bairro, $cidade, $estado, $cep);
			$endereco->setId($id);
			return $endereco;
		}
	}
	
	public function pesquisarComFiltro($texto, $filtro) {
		
		include_once("../conexao/config.php");
		$conexao = new Config();
		
		$query = "select * from endereco where " . $filtro . " like ?";
		
		$stmt = $conexao->conectar()->prepare($query);				

		$stmt->bindValue(1, '%' . $texto . '%');
		
		$stmt->execute();
		
		return $stmt;
	}
	
	public function pesquisarUltimos() {
		
		include_once("../conexao/config.php");
		$conexao = new Config();
		
		$query = "select * from endereco order by id_endereco DESC limit 0, 10";
		
		$stmt = $conexao->conectar()->prepare($query);				
		
		$stmt->execute();
		
		return $stmt;
	}
	
	public function listarEndereco() {
		
		include_once("../conexao/config.php");
		$conexao = new Config();
		
		$query = "SELECT DISTINCT (bairro) FROM endereco order by bairro;";
		
		$stmt = $conexao->conectar()->prepare($query);						
		
		$stmt->execute();
		
		return $stmt;
	}
	
	public function pesquisarEndBairro($bairro) {
		
		include_once("../conexao/config.php");
		$conexao = new Config();
		
		$query = "select * from endereco where bairro = ? order by logradouro";
		
		$stmt = $conexao->conectar()->prepare($query);	
		
		$stmt->bindValue(1, $bairro);					
		
		$stmt->execute();
		
		return $stmt;
	}
	
	public function remover($id) {
		
		include_once("../conexao/config.php");
		$conexao = new Config();
		
		$query = "delete from endereco where id_endereco=?";
		
		$stmt = $conexao->conectar()->prepare($query);
		
		$stmt->bindValue(1, $id);
		
		$stmt->execute();
	}

}

?>